//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using System.Linq;

namespace Mindscape.BackgroundMotion.Core.Model
{
  /// <summary>
  /// A static class which acts as a gateway to the active repositories managed in the system
  /// </summary>
  /// <remarks>
  /// This allows the usage of Repository<Entity>.Method() 
  /// </remarks>
  /// <typeparam name="T"></typeparam>
  public static class Repository<T>
    where T : class, IIdentifiable
  {
    /// <summary>
    /// Returns a count of the items in the repository
    /// </summary>
    /// <returns></returns>
    public static int Count()
    {
      return Current.Count();
    }

    /// <summary>
    /// Returns a count of the items in the repository which match the supplied lambda
    /// </summary>
    /// <param name="expression">A lambda expression which will be evaluated against the repository</param>
    /// <returns></returns>
    public static int Count(Expression<Func<T, bool>> expression)
    {
      return Current.Count(expression);
    }

    /// <summary>
    /// Finds a single instance of an entity matching a unique identifier.
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static T FindOne(int id)
    {
      return Current.FindOne(id);
    }

    /// <summary>
    /// Returns a requeryable set of all entities in the repository matching a unique identifier
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static IQueryable<T> Find(int id)
    {
      return Current.Find(id);
    }

    /// <summary>
    /// Returns a requeryable set of all entities in the repository matching a lambda expression
    /// </summary>
    /// <param name="expression"></param>
    /// <returns></returns>
    public static IQueryable<T> Find(Expression<Func<T, bool>> expression)
    {
      return Current.Find(expression);
    }

    /// <summary>
    /// Returns a requeryable set of all entities in the repository
    /// </summary>
    /// <returns></returns>
    public static IQueryable<T> Find()
    {
      return Current.Find();
    }

    /// <summary>
    /// Finds a single instance of an entity matching a lambda expression
    /// </summary>
    /// <param name="expression"></param>
    /// <returns>
    /// The first entity which matches the expession
    /// </returns>
    public static T FindOne(Expression<Func<T, bool>> expression)
    {
      return Current.FindOne(expression);
    }

    /// <summary>
    /// Tries to find a single instane of an entity matching a lambda expression
    /// </summary>
    /// <param name="expression">The expression.</param>
    /// <param name="entity">The entity which has been found.</param>
    /// <returns>true if the FindOne call succeeded, else false</returns>
    public static bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
    {
      return Current.TryFindOne(expression, out entity);
    }

    /// <summary>
    /// Finds all entities in the repository
    /// </summary>
    /// <returns></returns>
    public static IList<T> FindAll()
    {
      return Current.FindAll();
    }

    /// <summary>
    /// Finds all entities matching a lambda expression
    /// </summary>
    /// <param name="expression">The expression.</param>
    /// <returns></returns>
    public static IList<T> FindAll(Expression<Func<T, bool>> expression)
    {
      return Current.FindAll(expression);
    }

    /// <summary>
    /// Adds a new entity in to the repository
    /// </summary>
    /// <param name="entity"></param>
    public static void Add(T entity)
    {
      Current.Add(entity);
    }

    /// <summary>
    /// Saves the specified entity back to the repository
    /// </summary>
    /// <param name="entity"></param>
    public static void Save(T entity)
    {
      Current.Save(entity);
    }

    /// <summary>
    /// Removes the specified entity from the repository
    /// </summary>
    /// <param name="entity"></param>
    public static void Remove(T entity)
    {
      Current.Remove(entity);
    }

    /// <summary>
    /// References the current instance of the repository which is providing for the generic type of T
    /// </summary>
    private static IRepository<T> Current      
    {
      get { return UnitOfWork.GetRepository<T>(); }
    }
  }
}